package com.changgou.file.controller;

import com.changgou.file.FastDFSfile;
import com.changgou.file.util.FastDFSClien;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class UploadController {

    @PostMapping("/upload")
    public String upload(@RequestParam(name="file")MultipartFile file) throws Exception {
        if(!file.isEmpty()){
            String fileName = file.getOriginalFilename();
            byte[] bytes = file.getBytes();
            String ext = StringUtils.getFilenameExtension(fileName);
            FastDFSfile dfSfile =new FastDFSfile(fileName,bytes,ext);
            String[] upload = FastDFSClien.upload(dfSfile);
            String src="http://192.168.200.128:8080/"+upload[0]+"/"+upload[1];
            return src;
        }
        return null;
    }
}
